<template>
  <ContentWrap>
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="page-title">
          <Icon icon="ep:document-checked" class="title-icon" />
          <div class="title-text">
            <h2>广告线索记录</h2>
            <p>查看和管理广告投放产生的线索数据</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索工作栏 -->
    <div class="search-container">
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="110px"
        label-position="left"
        class="search-form"
      >
        <div class="search-row">
          <el-form-item label="广告主名称" prop="advertiserName" class="search-item">
            <el-input
              v-model="queryParams.advertiserName"
              placeholder="请输入广告主名称"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:user" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="广告计划名称" prop="promotionName" class="search-item">
            <el-input
              v-model="queryParams.promotionName"
              placeholder="请输入广告计划名称"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:document" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="线索名称" prop="clueName" class="search-item">
            <el-input
              v-model="queryParams.clueName"
              placeholder="请输入线索名称"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:user-filled" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="创建时间" prop="createTime" class="search-item">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="search-date-picker"
            >
              <template #prefix-icon>
                <Icon icon="ep:calendar" class="input-icon" />
              </template>
            </el-date-picker>
          </el-form-item>
        </div>

        <div class="search-actions">
          <el-button class="search-btn" @click="handleQuery">
            <Icon icon="ep:search" class="btn-icon" />
            <span class="btn-text">搜索</span>
          </el-button>
          <el-button class="reset-btn" @click="resetQuery">
            <Icon icon="ep:refresh" class="btn-icon" />
            <span class="btn-text">重置</span>
          </el-button>
          <el-button
            type="warning"
            class="export-btn"
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['ad:clue-log:export']"
          >
            <Icon icon="ep:download" class="btn-icon" />
            <span class="btn-text">导出</span>
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 列表容器 -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        class="clue-table"
      >
        <el-table-column label="广告主" align="left" prop="advertiserName" min-width="200">
          <template #default="scope">
            <div class="advertiser-cell">
              <Icon icon="ep:user" class="advertiser-icon" />
              <div class="advertiser-info">
                <div class="advertiser-name">{{ scope.row.advertiserName }}</div>
                <div class="advertiser-id">({{ scope.row.advertiserId }})</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="广告计划" align="left" prop="promotionName" min-width="200">
          <template #default="scope">
            <div class="promotion-cell">
              <Icon icon="ep:document" class="promotion-icon" />
              <div class="promotion-info">
                <div class="promotion-name">{{ scope.row.promotionName }}</div>
                <div class="promotion-id">({{ scope.row.promotionId }})</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="线索名称" align="center" prop="clueName" min-width="120">
          <template #default="scope">
            <div class="clue-cell">
              <Icon icon="ep:user-filled" class="clue-icon" />
              <span class="clue-text">{{ scope.row.clueName }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="电话" align="center" prop="telephone" width="140">
          <template #default="scope">
            <div class="phone-cell">
              <Icon icon="ep:phone" class="phone-icon" />
              <span class="phone-text">{{ scope.row.telephone }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="线索类型" align="center" prop="clueType" width="100">
          <template #default="scope">
            <el-tag type="primary" class="type-tag">
              {{ scope.row.clueType }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="分配类型" align="center" prop="allocationStatus" width="100">
          <template #default="scope">
            <el-tag type="success" class="allocation-tag">
              {{ scope.row.allocationStatus }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="互动场景" align="center" prop="actionType" width="100">
          <template #default="scope">
            <el-tag type="info" class="action-tag">
              {{ scope.row.actionType }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="年龄" align="center" prop="age" width="80">
          <template #default="scope">
            <div class="age-cell">
              <Icon icon="ep:user" class="age-icon" />
              <span class="age-text">{{ scope.row.age }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="省市县" align="center" prop="provinceName" width="180">
          <template #default="scope">
            <div class="location-cell">
              <Icon icon="ep:location" class="location-icon" />
              <div class="location-text">
                {{ scope.row.provinceName }} / {{ scope.row.cityName }} / {{ scope.row.countyName }}
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="详细地址" align="center" prop="address" width="180">
          <template #default="scope">
            <div class="address-cell">
              <Icon icon="ep:map-location" class="address-icon" />
              <span class="address-text">{{ scope.row.address }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="200"
          :formatter="dateFormatter"
        >
          <template #default="scope">
            <div class="time-cell">
              <Icon icon="ep:clock" class="time-icon" />
              <span class="time-text">{{
                dateFormatter(scope.row, scope.column, scope.row.createTime)
              }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
          class="custom-pagination"
        />
      </div>
    </div>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ClueLogForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import * as ClueLogApi from '@/api/ad/cluelog'
import ClueLogForm from './ClueLogForm.vue'
import { dateFormatter } from '@/utils/formatTime'
defineOptions({ name: 'AdClueLog' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advertiserName: null,
  promotionName: null,
  clueType: null,
  allocationStatus: null,
  actionType: null,
  clueName: null,
  telephone: null,
  clueOwnerName: null,
  provinceName: null,
  cityName: null,
  countyName: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ClueLogApi.getClueLogPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ClueLogApi.deleteClueLog(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ClueLogApi.exportClueLog(queryParams)
    download.excel(data, '广告线索记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 页面头部
.page-header {
  margin-bottom: 24px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #0052cc 0%, #0066ff 100%);
  border-radius: 16px;
  color: white;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  display: flex;
  align-items: center;
  gap: 16px;
}

.title-icon {
  font-size: 32px;
  color: rgba(255, 255, 255, 0.9);
}

.title-text {
  h2 {
    font-size: 24px;
    font-weight: 600;
    margin: 0 0 4px 0;
    color: white;
  }

  p {
    font-size: 14px;
    margin: 0;
    color: rgba(255, 255, 255, 0.8);
  }
}

// 搜索容器
.search-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.search-form {
  .search-row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
  }

  .search-item {
    flex: 1;
    min-width: 260px;
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
    }
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #0052cc;
      box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #0052cc;
      box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.search-btn,
.reset-btn,
.export-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 16px;
  }

  &:hover {
    transform: translateY(-1px);
  }
}

.search-btn {
  background: #f3f4f6;
  border: 2px solid #e5e7eb;
  color: #374151;

  &:hover {
    background: #e5e7eb;
    border-color: #d1d5db;
  }
}

.reset-btn {
  background: #fef3c7;
  border: 2px solid #fbbf24;
  color: #92400e;

  &:hover {
    background: #fde68a;
    border-color: #f59e0b;
  }
}

.export-btn {
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
  border: none;
  color: white;

  &:hover {
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
  }
}

// 表格容器
.table-container {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.clue-table {
  :deep(.el-table__header) {
    background: #f8fafc;

    th {
      background: #f8fafc !important;
      color: #374151;
      font-weight: 600;
      border-bottom: 2px solid #e5e7eb;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.2s ease;

      &:hover {
        background: #f8fafc !important;
      }

      td {
        padding: 16px 12px;
        border-bottom: 1px solid #f1f5f9;
      }
    }
  }
}

// 表格单元格样式
.advertiser-cell {
  display: flex;
  align-items: flex-start;
  gap: 8px;

  .advertiser-icon {
    color: #0052cc;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .advertiser-info {
    .advertiser-name {
      font-weight: 500;
      color: #374151;
      margin-bottom: 4px;
    }

    .advertiser-id {
      font-size: 12px;
      color: #6b7280;
      font-family: 'Courier New', monospace;
    }
  }
}

.promotion-cell {
  display: flex;
  align-items: flex-start;
  gap: 8px;

  .promotion-icon {
    color: #10b981;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .promotion-info {
    .promotion-name {
      font-weight: 500;
      color: #374151;
      margin-bottom: 4px;
    }

    .promotion-id {
      font-size: 12px;
      color: #6b7280;
      font-family: 'Courier New', monospace;
    }
  }
}

.clue-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .clue-icon {
    color: #8b5cf6;
    font-size: 14px;
  }

  .clue-text {
    color: #374151;
    font-weight: 500;
  }
}

.phone-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  .phone-icon {
    color: #10b981;
    font-size: 14px;
  }

  .phone-text {
    color: #374151;
    font-weight: 500;
    font-family: 'Courier New', monospace;
  }
}

.type-tag,
.allocation-tag,
.action-tag {
  border-radius: 6px;
  font-weight: 500;
  font-size: 12px;
}

.age-cell {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;

  .age-icon {
    color: #f59e0b;
    font-size: 12px;
  }

  .age-text {
    color: #374151;
    font-weight: 500;
  }
}

.location-cell {
  display: flex;
  align-items: flex-start;
  gap: 6px;

  .location-icon {
    color: #ef4444;
    font-size: 14px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .location-text {
    color: #374151;
    font-size: 13px;
    line-height: 1.4;
  }
}

.address-cell {
  display: flex;
  align-items: flex-start;
  gap: 6px;

  .address-icon {
    color: #6366f1;
    font-size: 14px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  .address-text {
    color: #374151;
    font-size: 13px;
    line-height: 1.4;
  }
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;

  .time-icon {
    color: #f59e0b;
    font-size: 14px;
  }

  .time-text {
    color: #6b7280;
    font-size: 13px;
  }
}

// 分页容器
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 24px;
  border-top: 1px solid #f1f5f9;
}

.custom-pagination {
  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;

      &.is-active {
        background: #0052cc;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
    }
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .page-header {
    padding: 12px 20px;
    margin-bottom: 16px;

    .title-text {
      h2 {
        font-size: 20px;
      }

      p {
        font-size: 13px;
      }
    }
  }

  .search-container {
    padding: 16px;

    .search-row {
      flex-direction: column;
      gap: 16px;

      .search-item {
        min-width: auto;
      }
    }

    .search-actions {
      justify-content: stretch;

      .search-btn,
      .reset-btn,
      .export-btn {
        flex: 1;
        justify-content: center;
      }
    }
  }

  .table-container {
    overflow-x: auto;

    .clue-table {
      min-width: 1400px;
    }
  }
}

@media (max-width: 480px) {
  .page-header {
    padding: 12px 16px;

    .page-title {
      gap: 12px;

      .title-icon {
        font-size: 24px;
      }

      .title-text {
        h2 {
          font-size: 18px;
        }
      }
    }
  }

  .search-container {
    padding: 12px;

    .search-actions {
      flex-direction: column;
      gap: 8px;
    }
  }

  .clue-table {
    :deep(.el-table__header) {
      th {
        padding: 12px 8px;
        font-size: 13px;
      }
    }

    :deep(.el-table__body) {
      tr td {
        padding: 12px 8px;
      }
    }
  }

  .btn-text {
    display: none;
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .search-container,
  .table-container {
    background: #1e293b;
    border-color: #334155;
  }

  .clue-table {
    :deep(.el-table__header) {
      th {
        background: #0f172a !important;
        color: #f1f5f9;
        border-color: #334155;
      }
    }

    :deep(.el-table__body) {
      tr {
        &:hover {
          background: #334155 !important;
        }

        td {
          border-color: #334155;
        }
      }
    }
  }

  .advertiser-info .advertiser-name,
  .promotion-info .promotion-name,
  .clue-cell .clue-text,
  .phone-cell .phone-text,
  .age-cell .age-text,
  .location-cell .location-text,
  .address-cell .address-text,
  .time-cell .time-text {
    color: #f1f5f9;
  }
}
</style>
